package com.example.appp;



import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;


public class Player extends View{
	private static final int STEP = 5;
    private final int EMPTINESS = 16;
    private static final int COUNT_HEIGHT_BLOCK = 24;
    private static final int COUNT_WIDTH_BLOCK = 16;
	
    public enum side{
        UP,DOWN,LEFT,RIGHT,ZERO
    }
    
	int positionX,positionY;
	side way;

	int life;
	boolean IsRight, IsLeft, IsUp, IsDown;
	Bitmap bitmapMan;

	public int [][]findMaps = new int[COUNT_WIDTH_BLOCK][COUNT_HEIGHT_BLOCK];
	public Player(Context context, int size) {
		super(context);
		way = side.LEFT;
		IsLeft=true;
		positionY=17*size;
		positionX=7*size;
		life=3;
		bitmapMan = BitmapFactory.decodeResource(getResources(), R.drawable.pman_4);
		
	}
	
	
	void onMove(int [][]a, int size){
		
		if (IsLeft){
	        if(
	        (positionX%size!=0||(a[positionY/size][positionX/size-1]==0||a[positionY/size][positionX/size-1]==1||a[positionY/size][positionX/size-1]==EMPTINESS) &&
	        (positionY%size==0||(a[positionY/size+1][positionX/size-1]==0||a[positionY/size+1][positionX/size-1]==1||a[positionY/size+1][positionX/size-1]==EMPTINESS))))
	                {
	                positionX-=STEP;
	                }
	}
	if (IsRight){
	        if(
	        (a[positionY/size][positionX/size+1]==0||a[positionY/size][positionX/size+1]==1||a[positionY/size][positionX/size+1]==EMPTINESS)&&
	        (positionY%size==0||(a[positionY/size+1][positionX/size+1]==0||a[positionY/size+1][positionX/size+1]==1||a[positionY/size+1][positionX/size+1]==EMPTINESS)))
	                {
	                positionX+=STEP;
	                }
	}
	if (IsDown){
	        if((a[positionY/size+1][positionX/size]==0||a[positionY/size+1][positionX/size]==1||a[positionY/size+1][positionX/size]==EMPTINESS)
	        && (positionX%size==0||(a[positionY/size+1][positionX/size+1]==0||a[positionY/size+1][positionX/size+1]==1||a[positionY/size+1][positionX/size+1]==EMPTINESS)))
	                {
	                positionY+=STEP;
	                }
	}
	if (IsUp){
	        if(positionY%size!=0||(a[positionY/size-1][positionX/size]==0||a[positionY/size-1][positionX/size]==1||a[positionY/size-1][positionX/size]==EMPTINESS)
	        &&(positionX%size==0||(a[positionY/size-1][positionX/size+1]==0||a[positionY/size-1][positionX/size+1]==1||a[positionY/size-1][positionX/size+1]==EMPTINESS)))
	                {
	                positionY-=STEP;
	                }
	}
		if (way==side.LEFT){if(
		        (positionX%size!=0||(a[positionY/size][positionX/size-1]==0||a[positionY/size][positionX/size-1]==1||a[positionY/size][positionX/size-1]==EMPTINESS) &&
		        (positionY%size==0||(a[positionY/size+1][positionX/size-1]==0||a[positionY/size+1][positionX/size-1]==1||a[positionY/size+1][positionX/size-1]==EMPTINESS))))
		                {
						IsLeft=true;IsRight=false;IsDown=false;IsUp=false;way=side.ZERO;
						
		                }
		}
	    if (way==side.RIGHT){if(
	            (a[positionY/size][positionX/size+1]==0||a[positionY/size][positionX/size+1]==1||a[positionY/size][positionX/size+1]==EMPTINESS)&&
	            (positionY%size==0||(a[positionY/size+1][positionX/size+1]==0||a[positionY/size+1][positionX/size+1]==1||a[positionY/size+1][positionX/size+1]==EMPTINESS)))
	                    {
	    				IsLeft=false;IsRight=true;IsDown=false;IsUp=false;way=side.ZERO;
	    				
	                    }
	    }	
	    if (way==side.UP){ 
	    	if(positionY%size!=0||(a[positionY/size-1][positionX/size]==0||a[positionY/size-1][positionX/size]==1||a[positionY/size-1][positionX/size]==EMPTINESS)
	            &&(positionX%size==0||(a[positionY/size-1][positionX/size+1]==0||a[positionY/size-1][positionX/size+1]==1||a[positionY/size-1][positionX/size+1]==EMPTINESS)))
	    	{
	    	IsLeft=false;IsRight=false;IsDown=false;IsUp=true;way=side.ZERO;
	    	
	    	}
	    }
	    if (way==side.DOWN){if((a[positionY/size+1][positionX/size]==0||a[positionY/size+1][positionX/size]==1||a[positionY/size+1][positionX/size]==EMPTINESS)
	            && (positionX%size==0||(a[positionY/size+1][positionX/size+1]==0||a[positionY/size+1][positionX/size+1]==1||a[positionY/size+1][positionX/size+1]==EMPTINESS)))
	    	{
	    	IsLeft=false;IsRight=false;IsDown=true;IsUp=false;way=side.ZERO;
	    	
	    	}
	    }
		if (positionX==(15*size) && positionY==(11*size) && IsRight)positionX=0;
		if (positionX==0 && positionY==(11*size) && IsLeft)positionX=(15*size);          

	}

	
    void onLoadImages(int size) {
        if (IsLeft) {
            bitmapMan = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pman_3);
        }
        if (IsRight) {
            bitmapMan = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pman_4);
        }
        if (IsDown) {
            bitmapMan = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pman_2);
        }
        if (IsUp) {
            bitmapMan = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pman_1);
        }
        if (positionX % (size / 2) == 0 && IsLeft) {
            bitmapMan = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pman_3_2);
        }
        if (positionX % (size / 2) == 0 && IsRight) {
            bitmapMan = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pman_4_2);
        }
        if (positionY % (size / 2) == 0 && IsUp) {
            bitmapMan = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pman_1_2);
        }
        if (positionY % (size / 2) == 0 && IsDown) {
            bitmapMan = BitmapFactory.decodeResource(getResources(),
                    R.drawable.pman_2_2);
        }

    }

    @SuppressLint("DrawAllocation")
    public void onDraw(Canvas canvas) {
        canvas.drawBitmap(bitmapMan, positionX, positionY, new Paint());
    }


}